package com.hotelManagerSystem.shiro;

import org.apache.commons.beanutils.ConvertUtils;
import org.apache.shiro.authz.Authorizer;
import org.apache.shiro.authz.ModularRealmAuthorizer;
import org.apache.shiro.authz.Permission;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.subject.PrincipalCollection;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

public class ShiroRealmAuthorizer extends ModularRealmAuthorizer {
    /*@Override
    public boolean isPermitted(PrincipalCollection principals, String permission) {
        assertRealmsConfigured();
        //所有realm
        Set<String> realmNames = principals.getRealmNames();
        //获取realm名字
        String realmName = realmNames.iterator().next();
        //循环比较自定义realm
        for (Realm realm:getRealms()){
            if (!(realm instanceof Authorizer)) continue;
            if (realmName.contains("Admin")){
                if (realm instanceof ShiroRealmAdmin){
                    return ((ShiroRealmAdmin)realm).isPermitted(principals,permission);
                }
            }else if(realmName.contains("Customer")){
                if (realm instanceof ShiroRealmCustomer){
                    return ((ShiroRealmCustomer)realm).isPermitted(principals,permission);
                }
            }
        }
        return false;
    }

    @Override
    public boolean isPermitted(PrincipalCollection principals, Permission permission) {
        assertRealmsConfigured();
        //所有realm
        Set<String> realmNames = principals.getRealmNames();
        //获取realm名字
        String realmName = realmNames.iterator().next();
        //循环比较自定义realm
        for (Realm realm:getRealms()){
            if (!(realm instanceof Authorizer)) continue;
            if (realmName.contains("Admin")){
                if (realm instanceof ShiroRealmAdmin){
                    return ((ShiroRealmAdmin)realm).isPermitted(principals,permission);
                }
            }else if(realmName.contains("Customer")){
                if (realm instanceof ShiroRealmCustomer){
                    return ((ShiroRealmCustomer)realm).isPermitted(principals,permission);
                }
            }
        }
        return false;
    }*/

    @Override
    public boolean hasRole(PrincipalCollection principals, String roleIdentifier) {
        assertRealmsConfigured();
        //所有realm
        Set<String> realmNames = principals.getRealmNames();
        //获取realm名字
        String realmName = realmNames.iterator().next();
        //循环比较自定义realm
        for (Realm realm:getRealms()){
            if (!(realm instanceof Authorizer)) continue;
            if (realmName.contains("Admin")){
                if (realm instanceof ShiroRealmAdmin){
                    return ((ShiroRealmAdmin)realm).hasRole(principals,roleIdentifier);
                }
            }else if(realmName.contains("Customer")){
                if (realm instanceof ShiroRealmCustomer){
                    return ((ShiroRealmCustomer)realm).hasRole(principals,roleIdentifier);
                }
            }
        }
        return false;
    }

}
